from db import utils
from logger import logger
from sub_events.base_event import BaseEvent
from tools.file_searcher import FileSearcher

LOGGER = logger.CustomLogger()

COMMENT_CHECK_MOD = "check mod"
COMMENT_FORCE_MOD = "force mod"

class CheckModEvent(BaseEvent):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.event_name_zh: str = "侵入式修改检查"
        self.event_name_en: str = "Intrusive Modification Check"

    def execute(self) :
        pr_number = self.hook.pr_number
        try:
            params = {'access_token': self.get_admin_token()}
            files = self.git_client.get_filenames(pr_number, params=params)
            whitelist = self.cfg.get("sub_events", {}).get("check_mod", {}).get("check_mod_whitelist", [])
            fs = FileSearcher(whitelist)
            if (fs.check_and_get_invalid_files(files) and
                    not utils.get_force_pass_status_by_hook(self.hook, "mod")):
                self.failed = True
        except Exception as e:
            LOGGER.error(f"[{self.hook.pr_number}] get files failed, details: {str(e)}")
            return
        if self.failed:
            suggestion = "请联系以下人员检视后评论\"%s\"。\n\n" % COMMENT_FORCE_MOD
            suggestion += "Please contact the following members to leave a \"%s\" comment.\n\n" % COMMENT_FORCE_MOD
            for member, name in self.cfg.get("sub_events", {}).get("check_mod", {}).get("force_accounts").items():
                suggestion += f"{name}: @{member.split('/')[-1]}\n"
            self.comment = suggestion
